package de.clue.corpus.twitter.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Tools {
	public static final String STRING_CONVERSION_CODE = "UTF8";
	 public static final String DEFAULT_CRYPTO_ALGORITHM;
	 private static final String CRYPTO_ALGORITHM_STARTED = "{";
	 private static final String CRYPTO_ALGORITHM_STOPPED = "}";
	 private static final String EMPTY_STRING = "";
	 private static final byte EMPTY_BYTE_ARRAY[] = new byte[0];
	 private static final String SUPPORTED_CRYPTO_ALGORITHMS[] = {"xor","sha"};
	 private static final byte BASE64_ENCODE_MAP[];
	 private static final byte BASE64_DECODE_MAP[];
	 static 
	 {
	     DEFAULT_CRYPTO_ALGORITHM = SUPPORTED_CRYPTO_ALGORITHMS[0];
	     byte abyte0[] = {
	         65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 
	         75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 
	         85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 
	         101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 
	         111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 
	         121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 
	         56, 57, 43, 47
	     };
	     BASE64_ENCODE_MAP = abyte0;
	     BASE64_DECODE_MAP = new byte[128];
	     for(int i = 0; i < BASE64_DECODE_MAP.length; i++)
	         BASE64_DECODE_MAP[i] = -1;

	     for(int j = 0; j < BASE64_ENCODE_MAP.length; j++)
	         BASE64_DECODE_MAP[BASE64_ENCODE_MAP[j]] = (byte)j;

	 }
	
	public static String getHash(String message){
		
		String algorithm = "SHA";
		return getHashWithAlgorithm(message,algorithm );
		
	}

	private static String getHashWithAlgorithm(String str, String algorithm) {
	     StringBuffer stringbuffer = new StringBuffer(CRYPTO_ALGORITHM_STARTED);
	     if(algorithm.length() == 0)
	     {
	         stringbuffer.append(CRYPTO_ALGORITHM_STOPPED).append(str);
	     } else
	     {
	         stringbuffer.append(algorithm.toUpperCase()).append(CRYPTO_ALGORITHM_STOPPED);
	         String s2 = null;
	         if(str.length() > 0)
	         {
	             byte abyte0[] = convert_to_bytes(str);
	             if(abyte0.length > 0)
	             {
	                 byte abyte1[] = null;
	                 try
	                 {
	                     abyte1 = encipher(abyte0, algorithm);
	                 }
	                 catch(Exception ex)
	                 {
	                	 ex.printStackTrace();
	                     return null;
	                 }
	                  if(abyte1 != null && abyte1.length > 0)
	                 {
	                     s2 = convert_viewable_to_string(abyte1);
	                     if(s2 == null)
	                         return null;
	                 }
	             }
	         }
	         if(s2 != null && s2.length() > 0)
	             stringbuffer.append(s2);
	     }
	     return stringbuffer.toString();
	 }
	
	
	private static byte[] encipher(byte[] abyte0, String algorithm) throws Exception {
		
		MessageDigest digest= MessageDigest.getInstance("SHA-256");
    	return digest.digest(abyte0);
    	
	}

	private static byte[] convert_to_bytes(String s)
	 {
	     byte abyte0[] = null;
	     if(s != null)
	         if(s.length() == 0)
	             abyte0 = EMPTY_BYTE_ARRAY;
	         else
	             try
	             {
	                 abyte0 = s.getBytes("UTF8");
	             }
	             catch(UnsupportedEncodingException ex)
	             {
	                 ex.printStackTrace();
	                 abyte0 = null;
	             }
	     return abyte0;
	 }
	
	 private static String convert_viewable_to_string(byte abyte0[])
	 {
	     String s = null;
	     if(abyte0 != null)
	         if(abyte0.length == 0)
	             s = EMPTY_STRING;
	         else
	             try
	             {
	                 s = convert_to_string(base64Encode(abyte0));
	             }
	             catch(Exception ex)
	             {
	            	 ex.printStackTrace();
	                 s = null;
	             }
	     return s;
	 }
	 
	 private static String convert_to_string(byte abyte0[])
	 {
	     String s = null;
	     if(abyte0 != null)
	         if(abyte0.length == 0)
	             s = EMPTY_STRING;
	         else
	             try
	             {
	                 s = new String(abyte0, "UTF8");
	             }
	             catch(UnsupportedEncodingException ex)
	             {
	            	 ex.printStackTrace();
	                 s = null;
	             }
	     return s;
	 }
	 
	 private static byte[] base64Encode(byte abyte0[])
	 {
	     byte abyte1[] = new byte[((abyte0.length + 2) / 3) * 4];
	     int i = 0;
	     int j = 0;
	     for(; i < abyte0.length - 2; i += 3)
	     {
	         abyte1[j++] = BASE64_ENCODE_MAP[abyte0[i] >>> 2 & 0x3f];
	         abyte1[j++] = BASE64_ENCODE_MAP[abyte0[i + 1] >>> 4 & 0xf | abyte0[i] << 4 & 0x3f];
	         abyte1[j++] = BASE64_ENCODE_MAP[abyte0[i + 2] >>> 6 & 3 | abyte0[i + 1] << 2 & 0x3f];
	         abyte1[j++] = BASE64_ENCODE_MAP[abyte0[i + 2] & 0x3f];
	     }

	     if(i < abyte0.length)
	     {
	         abyte1[j++] = BASE64_ENCODE_MAP[abyte0[i] >>> 2 & 0x3f];
	         if(i < abyte0.length - 1)
	         {
	             abyte1[j++] = BASE64_ENCODE_MAP[abyte0[i + 1] >>> 4 & 0xf | abyte0[i] << 4 & 0x3f];
	             abyte1[j++] = BASE64_ENCODE_MAP[abyte0[i + 1] << 2 & 0x3f];
	         } else
	         {
	             abyte1[j++] = BASE64_ENCODE_MAP[abyte0[i] << 4 & 0x3f];
	         }
	     }
	     for(; j < abyte1.length; j++)
	         abyte1[j] = 61;

	     return abyte1;
	 }
	

}
